package com.shengyo.model.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.shengyo.model.bean.Pcategory;
import com.shengyo.model.dao.IPcategoryDAO;

public class PcategoryDAOImpl extends BaseDAOImpl implements IPcategoryDAO{

	
	@SuppressWarnings("unchecked")
	public List<Pcategory> findAll() {
		return this.getHibernateTemplate().find("from pcategory");
	}

	
	@SuppressWarnings("unchecked")
	public List<Pcategory> findAll(final int first, final int max) {
		
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				return session.createQuery("from pcategory").setFirstResult(first).setMaxResults(max).list();
			}
		});
	}

	
	public Object findById(int id) {
		return null;
	}

	public void delete(Pcategory pcategory) {
		this.getHibernateTemplate().delete(pcategory);
		
	}

	
	@SuppressWarnings("unchecked")
	public Pcategory findByName(String name) {
		List list = this.getHibernateTemplate().find("from pcategory p where p.name=?",new Object[]{name});
		if (list != null && !list.isEmpty())
			return (Pcategory) list.get(0);
		else
			return null;
	}


	@Override
	public Pcategory findById(String id) {
		return (Pcategory) this.getHibernateTemplate().load(Pcategory.class, id);
	}

	

}
